<?php
namespace app\services;

class PmpListService extends BaseService {
    public function getPmpList() {
        $pmps = $this->getAll('FlowPmp', ['status' => 2]); 
        $adspaces = $this->getPmpAdspaces();
        $showPmps = [];
        foreach ($pmps as &$pmp) {
            $guid = $pmp['guid'];
            $showPmp = [
                'id'        => $guid,
                'name'      => $pmp['name'],
                'price'     => $pmp['pmp_price'],
                'resource'  => '',
            ];
            if (isset($adspaces[$guid])) {
                $showPmp['resource'] = implode(',', $adspaces[$guid]);
            }
            $showPmps[] = $showPmp;
        }
        $this->jsonReturn(['data' => $showPmps]);
    }

    public function getPmpAdspaces() {
        $adspaces = $this->getAll('FlowPmpAdspaceMap', ['status' => 1]);
        return $this->arrayGroup($adspaces, 'flow_pmp_guid', ['adspace_title']);
    }

    public function syncPmp() {
        $ssp = new SspService('vamaker');        
        $ssp->syncPmp();
        return $this->ok($this->t('pmp', 'PMP syncronized!'));
    }
}// end


